package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seckill.TbSeckillGoods;
import com.dongdongshop.pojo.seckill.TbSeckillOrder;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.seckill.SeckillServiceI;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seckill")
public class SeckillController {

   @Reference
    private SeckillServiceI seckillServiceI;

   private final Logger logger= LoggerFactory.getLogger(SeckillController.class);

    //跳转到商品秒杀页面
    @RequestMapping("index")
    public String index(Model model){
        List<TbSeckillGoods> seckillGoodsList=seckillServiceI.findSeckillList();
        model.addAttribute("seckillGoodsList",seckillGoodsList);
        return "seckill-index";
    }

    //根据id获取秒杀商品
    @RequestMapping("item/{id}")
    public String item(@PathVariable("id")Long id,Model model){
        TbSeckillGoods seckillGoods=seckillServiceI.findSeckillGoodsFromRedis(id);
        if (seckillGoods == null){
            return "redirect:/seckill/sellover";
        }
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";
    }

    //跳转商品售磐页面
    @RequestMapping("sellover")
    public String sellover(){
        return "sellover";
    }


    //立即购买
    @RequestMapping("createOrder")
    @ResponseBody
    public Result createOrder(Long id){
        //创建订单 1.商品信息 2.登录状态 3.库存 4.结束时间
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if(null == user){
            return Result.response(ResultEnum.UNATUTH).setData("未登录，请登录");
        }
        try{
            seckillServiceI.createOrder(id,user.getId());
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.FAIL).setData("商品已售罄");
        }
        return Result.response(ResultEnum.OK);
    }

    //从redis中根据userId获取订单id
    @RequestMapping("findOrder")
    @ResponseBody
    public Result findOrder(){
        try {
            //获取用户userId
            TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
            //根据用户查询商品id
            TbSeckillOrder seckillOrder =seckillServiceI.findOrderFromRedis(user.getId());
            Map<String,String> map=new HashMap<>();
            if (seckillOrder == null){//
                return Result.response(ResultEnum.FAIL);
            }else {
               map.put("out_trade_no",seckillOrder.getId().toString());
               map.put("total_amount",seckillOrder.getMoney().toString());
               map.put("userId",seckillOrder.getUserId());
               return Result.response(ResultEnum.OK).setData(map);
            }
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }
}
